package lion.web.interceptors.rewrite;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lion.framework.core.web.request.RequestHandler;
import lion.framework.core.web.request.RequestInterceptor;
import lion.web.OutputCachedResponse;

import org.apache.commons.lang.StringUtils;

/**
 * @author lion
 * @email hl_0074@sina.com
 * @date 2014年5月7日
 */
public class PageURLRewriteInterceptor implements RequestInterceptor {

	@Override
	public void intercept(HttpServletRequest request, HttpServletResponse response, RequestHandler rh) throws Exception {

		OutputCachedResponse ocr = new OutputCachedResponse(response);
		rh.handle(request, ocr);
		if (StringUtils.contains(response.getContentType(), "text/html")) {
			String html = ocr.getStringContent();
			html = LinkURLRewriter.rewrite(html);
			response.getWriter().write(html);
		} else {
			response.getOutputStream().write(ocr.getContent());
		}
	}
}
